<?php
namespace app\common\model\tools;

use think\Model;

/*************************************************
 * ClassName:     Code
 * Description:   验证码模型
 * Others:
 *************************************************/
class Captcha extends Model
{
    /**
     * getNum 得到数字验证码
     * @karl
     * @DateTime 2016-08-12T09:29:04+0800
     * @param    int                   $width   宽
     * @param    int                   $height  高
     * @return   void
     */
    static public function getNum($width = 70, $height = 24)
    {
        $str = "1,2,3,4,5,6,7,8,9,0";
        $list = explode(",", $str);
        $cmax = count($list) - 1;
        $var = '';
        for ($i = 0; $i < 4; $i++) {
            $ran = mt_rand(0, $cmax);
            $var .= $list[$ran];
        }
        session('captcha', $var);
        $im = imagecreate($width, $height);
        $hong = imagecolorallocate($im, 255, 0, 0);
        $lan = imagecolorallocate($im, 0, 0, 255);
        $lv = imagecolorallocate($im, 0, 128, 0);
        $zi = imagecolorallocate($im, 128, 0, 128);
        $hei = imagecolorallocate($im, 0, 0, 0);
        $bai = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $hei);
        imagestring($im, 5, 15, 4, $var, $bai);
        for ($i = 0; $i < 15; $i++) {
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $bai);
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $hong);
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $lv);
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $lan);
        }
        for ($i = 0; $i < 5; $i++) {
            imageline($im, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $zi);
        }
        //设置头部，image/png
        header('Content-Type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
    /**
     * getEquation 得到等式
     * @颜林梧
     * @param    int                   $w 图片宽度
     * @param    int                   $h 图片高度
     * @return   boolean                 
     */
    static public function getEquation($w = '100' , $h = '41')
    {
        $im = imagecreate($w, $h);
        //imagecolorallocate($im, 14, 114, 180); // background color
        $red = imagecolorallocate($im, 255, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);
    
        $num1 = rand(1, 20);
        $num2 = rand(1, 20);
        $a = $num1 + $num2;
        session('captcha',$a);

        $gray = imagecolorallocate($im, 118, 151, 199);
        $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
    
        //画背景
        imagefilledrectangle($im, 0, 0, 100, 43, $black);
        //在画布上随机生成大量点，起干扰作用;
        for ($i = 0; $i < 80; $i++) {
            imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
        }
    
        imagestring($im, 5, 5, 10, $num1, $red);
        imagestring($im, 5, 30, 8, "+", $red);
        imagestring($im, 5, 45, 10, $num2, $red);
        imagestring($im, 5, 70, 9, "=", $red);
        imagestring($im, 5, 80, 8, "?", $white);
    
        header("Content-type: image/png");
        imagepng($im);
        imagedestroy($im);
    }
}
